﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Threading;
using Ooganizer.API;
using Ooganizer.Server;

namespace Ooganizer.Client.UI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ArtifactWall : Window
    {
        object CreateImage(string Path)
        {
            Image myImage1 = new Image();
            myImage1.Height = HEIGHT;
            myImage1.Width = WIDTH;
            BitmapImage myBitmapImage1 = new BitmapImage();
            myBitmapImage1.BeginInit();
            myBitmapImage1.UriSource = new Uri(Path);
            myBitmapImage1.DecodePixelHeight = HEIGHT;
            myBitmapImage1.DecodePixelWidth = WIDTH;            
            myBitmapImage1.EndInit();
            myImage1.Source = myBitmapImage1;

            //myImage1.Margin = new Thickness(25);
            return myImage1;
        }

        Path CreatePath()
        {
            LineGeometry myLineGeometry = new LineGeometry();
            myLineGeometry.StartPoint = new Point(10, 50);
            myLineGeometry.EndPoint = new Point(1600,50);

            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;
            myPath.Data = myLineGeometry;
            return myPath;
        }

        void AddWorkspaceArtifacts(string workspaceName)
        {
            using (OoganizerState oogyState = new OoganizerState())
            {
                oogyState.SetCurrentWorkspace(workspaceName);

                using (OoganizerAPIProxy proxy = oogyState.GetAPIProxy())
                {
                    Workspace workspace = proxy.API.GetWorkspace(workspaceName);

                    foreach (OoganizerDAL.Artifact artifact in workspace.Artifacts)
                    {
                        xamCarouselPanel1.ChildElements.Add(CreateImage(artifact.Snapshot_Path));
                    }
                }
            }
        }

        public ArtifactWall(Dispatcher disp, string workspaceName)
        {
            InitializeComponent();

            WindowInteropHelper hInter = new WindowInteropHelper(this);
            int width = System.Windows.Forms.Screen.FromHandle(hInter.Handle).Bounds.Width;
            int height = System.Windows.Forms.Screen.FromHandle(hInter.Handle).Bounds.Height;

            Height = height;
            Width = width;
            grid1.Height = height;
            grid1.Width = width;
            
            xamCarouselPanel1.ViewSettings.ItemSize = new Size(WIDTH,HEIGHT);
            xamCarouselPanel1.ViewSettings.ItemPath = CreatePath();
            xamCarouselPanel1.ViewSettings.ItemsPerPage = 3;

            AddWorkspaceArtifacts(workspaceName);
        }

        private int Show_ArtifactWall()
        {
            try
            {                
               Show();               
            }
            catch (Exception){}
            return 0;
        }

        private void OnClosed(object sender, EventArgs e)
        {
            LaunchWall.KillWallThread();
        }

        #region datamembers
        
        const int WIDTH = 400;
        const int HEIGHT = 640;
        
        private delegate void delegate_void();

        #endregion
    }
}

